package com.donzell.controller;

import java.util.Arrays;

import javax.servlet.http.HttpSession;

import org.apache.commons.codec.DecoderException;
import org.apache.commons.codec.binary.Hex;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import com.donzell.auth.SessionHelper;
import com.donzell.dao.UserRepository;
import com.donzell.exception.LoginErrorException;
import com.donzell.model.bean.User;
import com.donzell.model.bean.UserSession;


@Controller
@RequestMapping(value="/", produces = "application/json;charset=UTF-8")
public class IndexController {
	Logger logger = LoggerFactory.getLogger(IndexController.class);
	@Autowired
	private UserRepository userRepository;
	
    @RequestMapping("/")
    public String index() {
    	// 加入一个属性，用来在模板中读取 HttpSession httpSession,ModelMap map
//    	boolean isLogined = UserSession.isUserLogin(httpSession);
//    	logger.info("index用户是否登录=>"+isLogined);
//    	if(isLogined){
//    		map.addAttribute("loginusername", "你好，"+UserSession.getLoginedUser(httpSession).getUsername()+"，欢迎你登录！");
//    	}else{
//    		map.addAttribute("loginusername","<a href='/login/tologin'>你还未登录，请先登录</a>");
//    	}
//		map.addAttribute("name", isLogined ? "用户已经登录" : "用户没有登录");
		return "index";
    	// return模板文件的名称，对应src/main/resources/templates/welcome.html
    }
	
	@RequestMapping("/err")
	public String err(HttpSession httpSession,ModelMap map) {
		logger.info("err用户是否登录=>"+UserSession.isUserLogin(httpSession));
		map.addAttribute("name", UserSession.isUserLogin(httpSession) ? "用户已经登录" : "用户没有登录");
		
		map.addAttribute("err_code", "err_code");
		map.addAttribute("err_message", "err_message");
		
		map.addAttribute("conform_url", "conform_url");
		map.addAttribute("cancel_url", "cancel_url");
		return "err";
	}
}
